package club.emergency.wechat.authorization.search.consumer.controller;

import club.emergency.wechat.authorization.search.consumer.service.AuthorizationSearchConsumer;
import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.utils.JsonConvert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.authorization.search.consumer.controller
 * Date: 2019/3/14
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@Api("权限查询接口")
@RestController
@RequestMapping("/authorization-search")
public class AuthorizationSearchController {

    private AuthorizationSearchConsumer authorizationSearchConsumer;

    public AuthorizationSearchController(AuthorizationSearchConsumer authorizationSearchConsumer) {
        this.authorizationSearchConsumer = authorizationSearchConsumer;
    }

    @ApiOperation("查询角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "当前页", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示的数据数", dataType = "int", paramType = "query")
    })
    @PostMapping("/flipListForRole")
    public ResponseEntity flipListForRole(
            @RequestParam(value = "roleName", required = false) String roleName,
            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(authorizationSearchConsumer.flipListForRole(roleName, pageNo, pageSize), ResponseEntity.class);
    }

    @ApiOperation("查询角色对应的权限列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "long", paramType = "query")
    })
    @PostMapping("/flipListAuthorizationByRoleId")
    public ResponseEntity flipListAuthorizationByRoleId(
            @RequestParam(value = "roleId") Long roleId
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(authorizationSearchConsumer.flipListAuthorizationByRoleId(roleId), ResponseEntity.class);
    }

    @ApiOperation("根据code查询权限列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "匹配字段", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", dataType = "long", paramType = "query")
    })
    @PostMapping("/flipListAuthorizationByCode")
    public ResponseEntity flipListAuthorizationByCode(
            @RequestParam(value = "code", required = false) Long code,
            @RequestParam(value = "parentId", required = false) Long parentId
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(authorizationSearchConsumer.flipListAuthorizationByCode(code, parentId), ResponseEntity.class);
    }
}
